package hafnium.bukkit.pail.pipe;

import hafnium.bukkit.pail.pipe.logging.PPLogger;
import hafnium.bukkit.pail.pipe.plugins.PailPlugin;
import hafnium.bukkit.pail.pipe.plugins.PipeProxy;
import hafnium.bukkit.util.Optimizer;

import java.util.ArrayList;

import org.bukkit.plugin.java.JavaPlugin;

public class PailPipe extends JavaPlugin {
	public static PailPipe instance = null;
	private final PPLogger logger = new PPLogger(this);

	ArrayList<PailPlugin> plugins;

	private Optimizer optimizer;

	/**
	 * Called by Bukkit when the plugin is disabled. Handles finalization,
	 * saving, etc.
	 */
	public void onDisable() {
		this.logger.log("Disabling...");

		this.logger.log("Disabled.");
	}

	/**
	 * Called by bukkit when the plugin is enabled. Handles all initialization.
	 */
	public void onEnable() {
		PailPipe.instance = this;

		this.logger.log("Enabling...");

		this.optimizer = new Optimizer();

		// TODO: Register asynch optimization.

		plugins = new ArrayList<PailPlugin>();
		
		this.logger.log("Enabled.");
	}

	/**
	 * Register a SubPail, returning the new Pail's PailProxy.
	 * 
	 * @param plugin
	 *            The SubPail to register.
	 * @return The SubPail's designated PipeProxy.
	 */
	public PipeProxy register(PailPlugin plugin) {
		plugins.add(plugin);

		this.logger.log("Registered " + plugin.getDescription().getName() + ".");

		return new PipeProxy(plugin);
	}

	public Optimizer getOptimizer() {
		return this.optimizer;
	}
}
